/**
 * @file Bsp_AirFan.c
 * @author 徐松亮 (5387603@qq.com)
 * @version 1.0
 * @date 2023-09-26
 *
 * @copyright Copyright (c) {2030}  沈阳迈思医疗科技有限公司
 *
 * @brief 驱动层-->风扇驱动
 */

/* 私有头部文件 (Includes)-----------------------------------------------------*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "Bsp_AirFan.h"
/* 编译警告条件 (Compile warning condition)------------------------------------*/
/* 私有类型定义 (Private typedef)----------------------------------------------*/
/* 私有型宏定义 (Private define)-----------------------------------------------*/
/* 私有常量定义 (Private constants)--------------------------------------------*/
/* 公有常量定义 (Public constants)---------------------------------------------*/
/* 私有变量定义 (Private variables)--------------------------------------------*/
/* 公有变量定义 (Public variables)---------------------------------------------*/
/* 私有函数声明 (Private function prototypes)----------------------------------*/
static void BspAirFan_Cycle_1ms(void *_tp_interface);
static void BspAirFan_Cycle_100ms(void *_tp_interface);
/* 私有函数实体 (Private functions)--------------------------------------------*/
/**
 * @brief   周期执行函数
 * @note    用于PwmIn
 * @param   None
 * @return  None
 * @todo    应放到RTC-1s中断中执行，减少CPU占用率
 */
static void BspAirFan_Cycle_1ms(void *_tp_interface)
{
    BSP_AIRFAN_T_INTERFACE *tp_interface = _tp_interface;
    tp_interface->t_Private.us_Timer_ms++;
    if (tp_interface->t_Private.us_Timer_ms >= 1000)
    {
        tp_interface->t_Private.us_Timer_ms = 0;
        //
        tp_interface->t_Output.us_Speed_Hz = tp_interface->t_Input.fnp_GetTimCmt();
        tp_interface->t_Input.fnp_SetTimCmt(0);
    }
}
/**
 * @brief   周期执行函数
 * @note    用于Debug测试等
 * @param   None
 * @return  None
 */
static void BspAirFan_Cycle_100ms(void *_tp_interface)
{
    char *pbuf = NULL;
    BSP_AIRFAN_T_INTERFACE *tp_interface = _tp_interface;
    // 开机自检
    {
        switch (tp_interface->t_Private.uc_SelfCheckStep)
        {
        case 0: // 风机开启
            tp_interface->t_Input.fnp_DebugOnCallback();
            tp_interface->t_Input.fnp_OnOff(true);
            tp_interface->t_Input.fnp_Da(500);
            tp_interface->t_Private.uc_SelfCheckCmt = 0;
            tp_interface->t_Private.uc_SelfCheckStep++;
            break;
        case 1: // 等待3秒
            tp_interface->t_Private.uc_SelfCheckCmt++;
            if (tp_interface->t_Private.uc_SelfCheckCmt >= 32)
            {
                tp_interface->t_Private.uc_SelfCheckStep++;
            }
            break;
        case 2: // 判断
            if ((50 <= tp_interface->t_Output.us_Speed_Hz) &&
                (tp_interface->t_Output.us_Speed_Hz <= 70))
            {
                tp_interface->t_Output.uc_FanError = false;
            }
            else
            {
                tp_interface->t_Output.uc_FanError = true;
            }
            //
            tp_interface->t_Private.uc_SelfCheckStep++;
            break;
        case 3: // 风机关闭
            tp_interface->t_Input.fnp_DebugOffCallback();
            tp_interface->t_Input.fnp_OnOff(false);
            tp_interface->t_Input.fnp_Da(0);
            tp_interface->t_Private.uc_SelfCheckStep++;
            break;
        default:
            break;
        }
        if (tp_interface->t_Private.uc_SelfCheckStep < 3)
        {
            return;
        }
    }
    // 初始化
    if (tp_interface->t_Private.uc_DebugTestEnable == 1)
    {
        tp_interface->t_Input.fnp_OnOff(true);
        //
#if 0
        tp_interface->t_Private.us_Timer_100ms++;
        if(tp_interface->t_Private.us_Timer_100ms<120)
        {
            BSP_AIRFAN_CONTROL((tp_interface->t_Private.us_Timer_100ms*10)*10);
        }
        else if(tp_interface->t_Private.us_Timer_100ms<240)
        {
            BSP_AIRFAN_CONTROL((2400-(tp_interface->t_Private.us_Timer_100ms*10))*10);
        }
        else if(tp_interface->t_Private.us_Timer_100ms>300)
        {
            tp_interface->t_Private.us_Timer_100ms=0;
        }
        else
        {
            ;
        }
#else
        tp_interface->t_Private.us_Timer_100ms++;
        if (tp_interface->t_Private.us_Timer_100ms == 1)
        {
            // 对应风机约为23转
            tp_interface->t_Input.fnp_Da(tp_interface->t_Private.us_TestDa1);
        }
        else if (tp_interface->t_Private.us_Timer_100ms == 50)
        {
            tp_interface->t_Input.fnp_Da(tp_interface->t_Private.us_TestDa2);
        }
        else if (tp_interface->t_Private.us_Timer_100ms == 100)
        {
            tp_interface->t_Private.us_Timer_100ms = 0;
        }
        //
        if ((tp_interface->t_Private.us_Timer_100ms % 10 == 0) && (tp_interface->t_Private.uc_DebugPrintEnable == 1))
        {
            pbuf = ModuleMemManager_Get(E_MEM_MANAGER_TYPE_256B);
            sprintf(pbuf, "DebugOut: BspFan_Speed_Hz-%d\r\n", tp_interface->t_Output.us_Speed_Hz);
            tp_interface->t_Input.fnp_DebugOutStr((int8_t *)pbuf);
            ModuleMemManager_Free(E_MEM_MANAGER_TYPE_256B, pbuf);
        }
#endif
    }
    else
    {
        tp_interface->t_Private.us_Timer_100ms = 0;
    }
    // 监测风机故障
    if (tp_interface->t_Output.uc_FanError == false)
    {
        if (tp_interface->t_Output.us_Read_Da > 300)
        {
            if (tp_interface->t_Output.us_Speed_Hz == 0)
            {
                tp_interface->t_Private.uc_FanErrTimer_100ms++;
            }
            else
            {
                tp_interface->t_Private.uc_FanErrTimer_100ms = 0;
            }
        }
        if (tp_interface->t_Private.uc_FanErrTimer_100ms > 20)
        {
            tp_interface->t_Private.uc_FanErrTimer_100ms = 0;
            tp_interface->t_Output.uc_FanError = true;
        }
    }
}
/* 公有函数实体 (Public functions)---------------------------------------------*/
void BspAirFan_Init(BSP_AIRFAN_T_INTERFACE *_tp_interface)
{
    _tp_interface->t_Output.fnp_OnOff = _tp_interface->t_Input.fnp_OnOff;
    _tp_interface->t_Output.fnp_Da = _tp_interface->t_Input.fnp_Da;
    //
    _tp_interface->t_Transplant.fnp_Cycle_1ms = BspAirFan_Cycle_1ms;
    _tp_interface->t_Transplant.fnp_Cycle_100ms = BspAirFan_Cycle_100ms;
}

/**
 * @brief
 * @param[in]  _tp_interfaceMy Param doc
 * @param[in]  _us_voltage_mvMy Param doc
 * @param[in]  _usp_res     My Param doc
 * @param[in]  _sp_temp     My Param doc
 */
void BspAirFan_Temp(BSP_AIRFAN_T_INTERFACE *_tp_interface, uint16_t _us_voltage_mv, uint16_t *_usp_res, int16_t *_sp_temp)
{
    uint32_t r_ohm = 0;
    int16_t temp;
    // mv -> r
    if (_tp_interface->t_Input.us_AdcReference_MV <= _us_voltage_mv)
    {
        _us_voltage_mv = _tp_interface->t_Input.us_AdcReference_MV;
    }
    if (_us_voltage_mv == _tp_interface->t_Input.us_AdcReference_MV)
    {
        r_ohm = 60000;
    }
    else
    {
        r_ohm = (3570L * _us_voltage_mv) / (_tp_interface->t_Input.us_AdcReference_MV - _us_voltage_mv);
        if (r_ohm >= 60000)
        {
            r_ohm = 60000;
        }
    }
    if (_usp_res != NULL)
    {
        *_usp_res = r_ohm;
    }
    // r  -> temp
    ModuleCount_NtcResistorTemp_Fun(
        ModuleCount_ResistorTemp_Map_B57421V2103J062,
        sizeof(ModuleCount_ResistorTemp_Map_B57421V2103J062) / 2, -10, r_ohm, &temp);
    if (_sp_temp != NULL)
    {
        *_sp_temp = temp;
    }
    //
    if (temp <= (-10))
    {
        _tp_interface->t_Output.uc_TempError = 1;
    }
    else if (temp >= (125))
    {
        _tp_interface->t_Output.uc_TempError = 2;
    }
    else
    {
        _tp_interface->t_Output.uc_TempError = 0;
    }
}

/**
 * @brief   Debug接口函数
 * @note    开启Debug相关指令后会循环打印
 * @param   _uc_trueOrFalse-判断指令进入与退出
 * @return  None
 */

void BspAirFan_DebugTestOnOff(uint8_t _uc_trueOrFalse, void *_tp_interface)
{
    BSP_AIRFAN_T_INTERFACE *tp_interface = _tp_interface;
    if (tp_interface == NULL)
    {
        return;
    }
    if (_uc_trueOrFalse == true)
    {
        tp_interface->t_Private.uc_DebugTestEnable = 1;
        tp_interface->t_Private.uc_DebugPrintEnable = 1;
        tp_interface->t_Private.us_TestDa1 = 500;
        tp_interface->t_Private.us_TestDa2 = 1500;
        tp_interface->t_Input.fnp_DebugOnCallback();
        tp_interface->t_Input.fnp_OnOff(true);
        {
            extern uint8_t BspAdc_DebugTest_Enable;
            BspAdc_DebugTest_Enable = 1;
        }
    }
    else
    {
        tp_interface->t_Private.uc_DebugTestEnable = 0;
        tp_interface->t_Private.uc_DebugPrintEnable = 0;
        tp_interface->t_Private.us_TestDa1 = 0;
        tp_interface->t_Private.us_TestDa2 = 0;
        tp_interface->t_Input.fnp_DebugOffCallback();
        tp_interface->t_Input.fnp_OnOff(false);
        {
            extern uint8_t BspAdc_DebugTest_Enable;
            BspAdc_DebugTest_Enable = 0;
        }
    }
}
void BspAirFan_TestOnOff(uint8_t _uc_trueOrFalse, void *_tp_interface, uint16_t _us_da)
{
    BSP_AIRFAN_T_INTERFACE *tp_interface = _tp_interface;
    if (_uc_trueOrFalse == true)
    {
        tp_interface->t_Private.uc_DebugTestEnable = 1;
        tp_interface->t_Private.us_TestDa1 = _us_da;
        tp_interface->t_Private.us_TestDa2 = _us_da;
        tp_interface->t_Input.fnp_DebugOnCallback();
        tp_interface->t_Input.fnp_OnOff(true);
    }
    else
    {
        tp_interface->t_Private.uc_DebugTestEnable = 0;
        tp_interface->t_Private.us_TestDa1 = 0;
        tp_interface->t_Private.us_TestDa2 = 0;
        tp_interface->t_Input.fnp_DebugOffCallback();
        tp_interface->t_Input.fnp_OnOff(false);
    }
}
/********************************END OF FILE***********************************/
